import { CODE_NO_USER, getYaliUserInfo, init } from '../../utils/checkAuth';
import Core from '../../yali-sdk/core/Core';
import getConfig from '../../config';
import ActionDispatcher from '../../yali-sdk/utils/ActionDispatcher';
import { addNotifier } from '../../yali-sdk/utils/NotifyRegistryCenter';
import { init as bInit, joinLiveGroup } from '../../utils/broadcastGroupHandler';
import { init as socketInit } from '../../yali-sdk/utils/initYaLiWebSocket';
import wxFetch from '../../yali-sdk/utils/wxFetch';

Page({
    onReady() {
        getConfig((config) => {
            // console.log('new ', config);
            Core.init(undefined, config.serverUrl);
            global.actionDispatcher = new ActionDispatcher(Core, wxFetch);
            init(global.actionDispatcher);
            bInit(global.actionDispatcher);
            socketInit(config.wsUrl);
            const cb = () => joinLiveGroup().then(() => {}, () => {});
            addNotifier('SOCET_CLOSE', cb, this);
            cb();
            console.log(config,'config');
            if (config.entryPage!=2){
            getYaliUserInfo().then((user) => {
                //判断入口是否正常进口(1:直播，2:审核页面)
                config.entryPage ==1? user && wx.reLaunch({ url: '/pages/anchor/list/list' }): user && wx.reLaunch({ url: '/pages/list/list' })
            }, (code) => {
                switch (code) {
                    case CODE_NO_USER: {
                        config.entryPage ==1?wx.reLaunch({ url: '/pages/regist/regist' }):wx.reLaunch({ url: '/pages/login/login' });
                        break;
                    }
                    default: break;
                }
            });
          }else{
              wx.redirectTo({ url: '/pages/list/list' })
          }
        });
    },
});
